{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow import keras\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data() # 加载fashion_mnist数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_image = train_image/255 # 归一化\n",
    "test_image = test_image/255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置输入\n",
    "input = keras.Input(shape=(28*28))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用Flatten，将Flatten看成一个函数\n",
    "x = keras.layers.Flatten()(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = keras.layers.Dense(32,activation='relu')(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = keras.layers.Dropout(0.5)(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = keras.layers.Dense(64,activation='relu')(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = keras.layers.Dense(10,activation='softmax')(x) # 输出层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.Model(inputs = input,outputs = output) # 依据指定的输入输出初始化模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"functional_3\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "input_4 (InputLayer)         [(None, 784)]             0         \n",
      "_________________________________________________________________\n",
      "flatten_3 (Flatten)          (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense_5 (Dense)              (None, 32)                25120     \n",
      "_________________________________________________________________\n",
      "dropout_4 (Dropout)          (None, 32)                0         \n",
      "_________________________________________________________________\n",
      "dense_6 (Dense)              (None, 64)                2112      \n",
      "_________________________________________________________________\n",
      "dense_7 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 27,882\n",
      "Trainable params: 27,882\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/30\n",
      "WARNING:tensorflow:Model was constructed with shape (None, 784) for input Tensor(\"input_4:0\", shape=(None, 784), dtype=float32), but it was called on an input with incompatible shape (32, 28, 28).\n",
      "WARNING:tensorflow:Model was constructed with shape (None, 784) for input Tensor(\"input_4:0\", shape=(None, 784), dtype=float32), but it was called on an input with incompatible shape (32, 28, 28).\n",
      "1869/1875 [============================>.] - ETA: 0s - loss: 0.8709 - accuracy: 0.6652WARNING:tensorflow:Model was constructed with shape (None, 784) for input Tensor(\"input_4:0\", shape=(None, 784), dtype=float32), but it was called on an input with incompatible shape (None, 28, 28).\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.8707 - accuracy: 0.6653 - val_loss: 0.5934 - val_accuracy: 0.7873\n",
      "Epoch 2/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.6637 - accuracy: 0.7481 - val_loss: 0.5479 - val_accuracy: 0.8039\n",
      "Epoch 3/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.6233 - accuracy: 0.7662 - val_loss: 0.5758 - val_accuracy: 0.7700\n",
      "Epoch 4/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.5968 - accuracy: 0.7752 - val_loss: 0.5778 - val_accuracy: 0.7744\n",
      "Epoch 5/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.5808 - accuracy: 0.7833 - val_loss: 0.5269 - val_accuracy: 0.8102\n",
      "Epoch 6/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5673 - accuracy: 0.7887 - val_loss: 0.5161 - val_accuracy: 0.8173\n",
      "Epoch 7/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.5499 - accuracy: 0.7965 - val_loss: 0.6014 - val_accuracy: 0.7499\n",
      "Epoch 8/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5386 - accuracy: 0.7999 - val_loss: 0.5229 - val_accuracy: 0.8112\n",
      "Epoch 9/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5332 - accuracy: 0.7998 - val_loss: 0.5424 - val_accuracy: 0.8053\n",
      "Epoch 10/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5272 - accuracy: 0.8051 - val_loss: 0.6014 - val_accuracy: 0.7628\n",
      "Epoch 11/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.5210 - accuracy: 0.8058 - val_loss: 0.6518 - val_accuracy: 0.7364\n",
      "Epoch 12/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5172 - accuracy: 0.8109 - val_loss: 0.5291 - val_accuracy: 0.8067\n",
      "Epoch 13/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5149 - accuracy: 0.8095 - val_loss: 0.6107 - val_accuracy: 0.7621\n",
      "Epoch 14/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5084 - accuracy: 0.8135 - val_loss: 0.5385 - val_accuracy: 0.7970\n",
      "Epoch 15/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5049 - accuracy: 0.8133 - val_loss: 0.5766 - val_accuracy: 0.7864\n",
      "Epoch 16/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.5018 - accuracy: 0.8153 - val_loss: 0.5574 - val_accuracy: 0.7983\n",
      "Epoch 17/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4959 - accuracy: 0.8171 - val_loss: 0.5335 - val_accuracy: 0.8045\n",
      "Epoch 18/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4897 - accuracy: 0.8198 - val_loss: 0.5889 - val_accuracy: 0.7773\n",
      "Epoch 19/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.4886 - accuracy: 0.8206 - val_loss: 0.5407 - val_accuracy: 0.8001\n",
      "Epoch 20/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4893 - accuracy: 0.8209 - val_loss: 0.5617 - val_accuracy: 0.7923\n",
      "Epoch 21/30\n",
      "1875/1875 [==============================] - 7s 4ms/step - loss: 0.4875 - accuracy: 0.8216 - val_loss: 0.5197 - val_accuracy: 0.8087\n",
      "Epoch 22/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4786 - accuracy: 0.8232 - val_loss: 0.5570 - val_accuracy: 0.7909\n",
      "Epoch 23/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4802 - accuracy: 0.8238 - val_loss: 0.5574 - val_accuracy: 0.7793\n",
      "Epoch 24/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4758 - accuracy: 0.8255 - val_loss: 0.5237 - val_accuracy: 0.8101\n",
      "Epoch 25/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4798 - accuracy: 0.8259 - val_loss: 0.5489 - val_accuracy: 0.7983\n",
      "Epoch 26/30\n",
      "1875/1875 [==============================] - 7s 3ms/step - loss: 0.4777 - accuracy: 0.8250 - val_loss: 0.5300 - val_accuracy: 0.8009\n",
      "Epoch 27/30\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.4781 - accuracy: 0.8252 - val_loss: 0.5630 - val_accuracy: 0.7954\n",
      "Epoch 28/30\n",
      "1875/1875 [==============================] - 6s 3ms/step - loss: 0.4739 - accuracy: 0.8246 - val_loss: 0.4990 - val_accuracy: 0.8206\n",
      "Epoch 29/30\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.4726 - accuracy: 0.8284 - val_loss: 0.5276 - val_accuracy: 0.8071\n",
      "Epoch 30/30\n",
      "1875/1875 [==============================] - 8s 4ms/step - loss: 0.4721 - accuracy: 0.8263 - val_loss: 0.5492 - val_accuracy: 0.8026\n"
     ]
    }
   ],
   "source": [
    "histroy = model.fit(train_image,\n",
    "                    train_label,\n",
    "                    epochs=30,\n",
    "                    validation_data=(test_image,test_label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
